package com.sfh.dog.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.pulltorefresh.PullToRefreshBase;
import com.pulltorefresh.PullToRefreshBase.OnRefreshListener2;
import com.pulltorefresh.PullToRefreshListView;
import com.sfh.dog.R;
import com.sfh.dog.ui.base.BaseFragment;
import com.sfh.dog.util.NetWorkHelper;

public abstract class AFragment extends BaseFragment
{
    
    public PullToRefreshListView mPullRefreshListView;
    
    public int page = 0;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(Layout(), container, false);
        mPullRefreshListView = (PullToRefreshListView)view.findViewById(R.id.lvContent);
        return view;
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        getInit();
        init();
        
    }
    
    public abstract void getData();
    
    public abstract void getInit();
    
    public abstract void select(int position);
    
    public abstract int Layout();
    
    private void init()
    {
        mPullRefreshListView.setScrollingWhileRefreshingEnabled(false);
        mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>()
        {
            
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView)
            {
                if (NetWorkHelper.isNetworkAvailable(getActivity()))
                {
                    page = 0;
                    getData();
                }
                else
                {
                    mPullRefreshListView.onRefreshComplete();
                }
                
            }
            
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView)
            {
                if (NetWorkHelper.isNetworkAvailable(getActivity()))
                {
                    page++;
                    getData();
                }
                else
                {
                    mPullRefreshListView.onRefreshComplete();
                }
            }
        });
        mPullRefreshListView.setOnItemClickListener(new OnItemClickListener()
        {
            
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                select(position);
            }
        });
        mPullRefreshListView.doPullRefreshing(800);
        
    }
    
    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }
    
}
